#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QPainter>
#include <QPixmap>
#include <QFile>
#include <QDebug>
#include <QMouseEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);  //  设置为无边框格式窗口
    this->setFixedSize(659, 370);

    // 载入样式
    QFile file(":/qss/main.qss");
    file.open(QIODevice::ReadOnly);
    QByteArray style = file.readAll();
    this->setStyleSheet(style);

    QPushButton *closeBtn = new QPushButton(this);
    closeBtn->setStyleSheet("border: 0;");
    closeBtn->resize(20, 20);
    closeBtn->move(639, 0);
    closeBtn->setIcon(QIcon(":/images/guanbi"));
    connect(closeBtn, &QPushButton::clicked, this, &Widget::close);

    QPushButton *minWindowBtn = new QPushButton(this);
    minWindowBtn->setStyleSheet("border: 0;");
    minWindowBtn->resize(20, 20);
    minWindowBtn->move(609, 0);
    minWindowBtn->setIcon(QIcon(":/images/minIcon"));
    connect(minWindowBtn, &QPushButton::clicked, this, &Widget::showMinimized);

    QPushButton *volumeBtn = new QPushButton(this);
    volumeBtn->setStyleSheet("border: 0;");
    volumeBtn->resize(20, 20);
    volumeBtn->move(574, 0);
    volumeBtn->setIcon(QIcon(":/images/volumeMiddle"));
    connect(volumeBtn, &QPushButton::clicked, [this, volumeBtn](){
        this->isMuted = !this->isMuted;
        this->setVolume(volumeBtn);
    });

}

void Widget::paintEvent(QPaintEvent *event)
{
    // 1. 绘制左侧的广告图片
    QPainter painter(this);
    QPixmap pix(":/images/ad");
    painter.drawPixmap(QRect(0, 0, pix.width(), pix.height()), pix);

    // 2. 绘制右侧的黑色背景
    QBrush brush(QColor(16, 17, 19));
    painter.setBrush(brush);
    painter.drawRect(QRect(pix.width(), 0, this->width() - pix.width(), this->height()));
}


// 音量控制
void Widget::setVolume(QPushButton *btn)
{
   if (isMuted)
   {
       btn->setIcon(QIcon(":/images/volumeCross"));
   }
   else
   {
       btn->setIcon(QIcon(":/images/volumeMiddle"));
   }
}

// 当鼠标按下时，记录在窗口中的坐标，并判断是否在可拖动区域内
void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        // 判断鼠标点击的地方是否是窗口的子部件
        QWidget *childWidget = childAt(event->pos());
        if (childWidget == nullptr || childWidget == this) {
            // 如果点击的是空白区域，开始拖动
            this->x = event->globalPos().x() - this->geometry().x();
            this->y = event->globalPos().y() - this->geometry().y();
            this->isDragging = true;
        }
    }
}

// 当鼠标移动时，判断是否是拖动状态
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (this->isDragging && (event->buttons() & Qt::LeftButton)) {
        // 移动窗口
        int x = event->globalX() - this->x;
        int y = event->globalY() - this->y;
        this->move(x, y);
    }
}

// 当鼠标释放时，结束拖动状态
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        this->isDragging = false;
    }
}

Widget::~Widget()
{
    delete ui;
}
